/**
 * shows.js
 */

var subsXHR;

var showTitle;
var showImage;
var showUrl;

var showId;

var cursorPosition = 1;

// XML Namespace resolver for XPath namespace resolution
function NSResolver(prefix) 
{
	if(prefix == 'media') 
	{
		return "http://search.yahoo.com/mrss/";
	}
	return null;
}

// Takes a definition document and populates the various DCTK controls
function handleSubsResult(doc)
{
    // extract information with XPath
	
	itemsNum = doc.evaluate("count(//item)", doc, NSResolver, XPathResult.ANY_TYPE, null).numberValue;
	
	buildDate = doc.evaluate("//channel/pubDate", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
	
	showTitle = new Array()
	for (var i = 1; i <= itemsNum; i++){
		showTitle[i] = doc.evaluate("//item[" + i + "]/title", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
		}
	showImage = new Array()
	for (var i = 1; i <= itemsNum; i++){
		showImage[i] = doc.evaluate("//item[" + i + "]/media:thumbnail/@url", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
		}
	showUrl = new Array()
	for (var i = 1; i <= itemsNum; i++){
		showUrl[i] = doc.evaluate("//item[" + i + "]/guid", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
		}
    
    var dctk = connected.toolkit;
	
	dateTxt = dctk.getElementById("dateTxt");
    dateTxt.text = "Last Updated: "+buildDate;
	
	createShowList()
}


function startSubsRequest()
{
    subsXHR = new XMLHttpRequest();
    subsXHR.onreadystatechange = onSubsXHRStateChange;
	subsXHR.open("GET", feed, true);
    subsXHR.send(null);
}

function onSubsXHRStateChange()
{
    if (subsXHR.readyState == 4)
    {
        if (subsXHR.status == 200)
        {
            handleSubsResult(subsXHR.responseXML);
        }
    }
}

function showSelected()
{
  cursorPosition = movieList.curSel;
  subsTitle = showTitle[cursorPosition+1];
  pageUrl = showUrl[cursorPosition+1].split('#');
  	
	if (feedTitle == "POPULAR SHOWS")
	{
		state = "popShows";
	}
	else state = "myShows";
	
	connected.debug.print(state);
	
  startShowRequest();
  
}

function createShowList(){
	var item;
	var dctk = connected.toolkit;
	for (var i = 1; i <= itemsNum; i++){
		item = movieList.addItem("id-0"+[i]);
    	item.text = showTitle[i];
		item.img.src = showImage[i];
		item.showText = true;
	}
}

function doShowsLayout()
{
    var dctk = connected.toolkit;

    dctk.logicalWidth = 640;
    dctk.logicalHeight = 480;
    
    // create root panel
	var root = dctk.createPanelCtrl("root", true);
	root.width = 640;
	root.height = 480;
	root.backgroundStyle = dctkIElement.BACKGROUND_STYLE_IMAGE;
	root.backgroundImg.src = "hulu-back.png"
	root.color.rgb = 0xFFFFFF;
	root.onUnhandledKeyEvent = mainButtonHandler;
	dctk.rootPanel = root;
	
	var header = dctk.createPanelCtrl("header", true);
	header.setRect(0, 413, 640, 480);
	header.filled = true;
	header.color.rgb = 0xf2f2f2;
	root.addChild(header);
	
	var footer = dctk.createPanelCtrl("footer", true);
	footer.width = 640;
	footer.height = 38;
	footer.filled = true;
	footer.color.rgb = 0xf2f2f2;
	root.addChild(footer);
	
	var logo = dctk.createImageCtrl("logo", true);
	logo.setRect(15, 415, 90, 478);
	logo.img.src = "hulu-logo.png";
	root.addChild(logo);
	
	var storage = connected.openStorage(huluStorage,true);
	queueId = storage.get(huluQueueId,queueId)

	showsLayout()
	startSubsRequest();	
	//connected.toolkit.debug.drawElementOutline = true;

}

function showsLayout(){
	
	var dctk = connected.toolkit;

    dctk.logicalWidth = 640;
    dctk.logicalHeight = 480;
    
    // create root panel
	var root = dctk.createPanelCtrl("root", true);
	root.width = 640;
	root.height = 480;
	root.backgroundStyle = dctkIElement.BACKGROUND_STYLE_IMAGE;
	root.backgroundImg.src = "hulu-back.png"
	root.color.rgb = 0xFFFFFF;
	root.onUnhandledKeyEvent = mainButtonHandler;
	dctk.rootPanel = root;
	
	var header = dctk.createPanelCtrl("header", true);
	header.setRect(0, 413, 640, 480);
	header.filled = true;
	header.color.rgb = 0xf2f2f2;
	root.addChild(header);
	
	var footer = dctk.createPanelCtrl("footer", true);
	footer.width = 640;
	footer.height = 38;
	footer.filled = true;
	footer.color.rgb = 0xf2f2f2;
	root.addChild(footer);
	
	var logo = dctk.createImageCtrl("logo", true);
	logo.setRect(15, 415, 90, 478);
	logo.img.src = "hulu-logo.png";
	root.addChild(logo);
	
	var queueTxt = dctk.createLabelCtrl("queueTxt", true);
    queueTxt.font = dctk.createFontByFacename("DIN-Bold", false, false);
    queueTxt.setRect(80, 380, 560, 415);
    queueTxt.textColor.rgb = 0x99cc33;
    queueTxt.fontSize = 25;
    queueTxt.align = dctkLabel.TS_CENTER;
	queueTxt.valign = dctkLabel.TS_MIDDLE;
	queueTxt.text = feedTitle;
    root.addChild(queueTxt);
	
	var dateTxt = dctk.createLabelCtrl("dateTxt", true);
    dateTxt.font = dctk.createFontByFacename("DIN-Bold", false, false);
    dateTxt.setRect(10, 10, 630, 30);
    dateTxt.textColor.rgb = 0x808080;
    dateTxt.fontSize = 20;
    dateTxt.align = dctkLabel.TS_LEFT;
	dateTxt.valign = dctkLabel.TS_MIDDLE;
	dateTxt.text = "Loading...";
    root.addChild(dateTxt);
	
	var footerTxt = dctk.createLabelCtrl("footerTxt", true);
    footerTxt.font = dctk.createFontByFacename("DIN-Bold", false, false);
    footerTxt.setRect(10, 10, 630, 30);
    footerTxt.textColor.rgb = 0x808080;
    footerTxt.fontSize = 20;
    footerTxt.align = dctkLabel.TS_RIGHT;
	footerTxt.valign = dctkLabel.TS_MIDDLE;
	footerTxt.text = "Press MENU for Options";
    root.addChild(footerTxt);
	
	movieList = dctk.createThumbCtrl("movieList", true);
    movieList.setRect(25, 38, 615, 385);
	movieList.thumbHCount = 4;
    movieList.thumbVCount = 3;
	movieList.fontSize = 14;
	movieList.thumbHGap = 0.08;
	movieList.thumbVGap = 0.08;
	movieList.marquee = true;
    movieList.keyWrap = true;
	movieList.showPageIndicator = true;
	movieList.textColor.rgb = 0xFFFFFF;
	movieList.textSelColor.rgb = 0x000000;
	movieList.labelColor.rgb = 0x000000;
	movieList.labelSelColor.rgb = 0xCCCCCC;
	movieList.onSelect = showSelected;
	root.addChild(movieList);
	
	dctk.screenFade(1, 0, 0.5);
	
}



